import requests
import json
import uuid
import sys

# 添加项目根目录到Python路径
import os
project_root = os.path.dirname(os.path.abspath(__file__))
sys.path.append(project_root)

# 从配置中获取信息
from backend.common.config import config_manager

print(f"config_manager.api.version = {config_manager.api.version}")
print(f"激活的LLM提供商 = {config_manager.llm_providers.active_provider}")

# 确定使用的路径 - 使用正确的API版本前缀
api_version = config_manager.api.version
test_path = f'http://localhost:8001/{api_version}/chat'

# 准备包含session_id的完整请求体
session_id = str(uuid.uuid4())
data = {
    'session_id': session_id,
    'message': 'hello',
    'use_rag': False,  # 禁用RAG以简化测试
    'use_tools': False,  # 禁用工具调用以简化测试
    'stream': True
}

headers = {'Content-Type': 'application/json'}

print(f"\n测试路径: {test_path}")
print(f"请求数据: {data}")

try:
    # 使用stream=True处理流式响应
    response = requests.post(test_path, headers=headers, data=json.dumps(data), timeout=30, stream=True)
    
    print(f"\n状态码: {response.status_code}")
    
    if response.status_code == 200:
        print("\nAPI请求成功！\n响应内容:")
        # 处理流式响应
        for chunk in response.iter_content(chunk_size=None):
            if chunk:
                # 解码并打印响应块
                chunk_str = chunk.decode('utf-8')
                print(chunk_str, end='', flush=True)
        print()
    else:
        print(f"\nAPI请求失败！\n详细信息: {response.text}")
        
        # 如果是500错误，让我们检查后端日志
        if response.status_code == 500:
            print("\n请查看后端服务日志获取详细错误信息。")
except Exception as e:
    print(f"\n发生错误: {e}")
